//
//  CourseHeaderView.swift
//  Auscultation
//
//  Created by 张文博 on 2021/3/20.
//

import Foundation

import UIKit

class CourseHeaderView: UIView {
    
    var btnClickClouse: ((_ imageTitleView: LGFImageTitleView) -> Void)?
    var startStudyClickClouse: (() -> Void)?

    lazy var topWhiteView: UIView = {
        let topWhiteView = UIView()
        self.addSubview(topWhiteView)
        topWhiteView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(4)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(90)
        }
        topWhiteView.setNeedsLayout()
        topWhiteView.layoutIfNeeded()
        // fillCode
        topWhiteView.layer.cornerRadius = 5
        // shadowCode
        topWhiteView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        topWhiteView.layer.shadowOffset = CGSize(width: 0, height: 2)
        topWhiteView.layer.shadowOpacity = 1
        topWhiteView.layer.shadowRadius = 5
        
        return topWhiteView
    }()
    
    lazy var bottomWhiteView: UIView = {
        let bottomWhiteView = UIView()
        bottomWhiteView.backgroundColor = .white
        return bottomWhiteView
    }()

    lazy var todayStudyL: UILabel = {
        let todayStudyL = UILabel()
        topWhiteView.addSubview(todayStudyL)
        todayStudyL.text = "今日已学"
        todayStudyL.font = UIFont.systemFont(ofSize: 14)
        todayStudyL.textColor = UIColor.init(lgf_HexString: "A0A0A0")
        todayStudyL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(60)
            make.top.equalToSuperview().offset(12)
        }
        return todayStudyL
    }()

    lazy var todayStudyVL: UILabel = {
        let todayStudyVL = UILabel()
        topWhiteView.addSubview(todayStudyVL)
        todayStudyVL.font = UIFont.systemFont(ofSize: 27)
        todayStudyVL.textColor = UIColor.init(lgf_HexString: "0F0F0F")
        todayStudyVL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(60)
            make.bottom.equalToSuperview().offset(-19)
        }
        return todayStudyVL
    }()

    lazy var punchcardL: UILabel = {
        let punchcardL = UILabel()
        topWhiteView.addSubview(punchcardL)
        punchcardL.font = UIFont.systemFont(ofSize: 14)
        punchcardL.textColor = UIColor.init(lgf_HexString: "A0A0A0")
        punchcardL.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-60)
            make.top.equalToSuperview().offset(12)
        }
        return punchcardL
    }()
    
    lazy var punchcardVL: UILabel = {
        let punchcardVL = UILabel()
        topWhiteView.addSubview(punchcardVL)
        punchcardVL.font = UIFont.systemFont(ofSize: 27)
        punchcardVL.textColor = UIColor.init(lgf_HexString: "0F0F0F")
        punchcardVL.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-60)
            make.bottom.equalToSuperview().offset(-19)
        }

        return punchcardVL
    }()
    
    lazy var studyView: UIImageView = {
        let studyView = UIImageView()
        self.addSubview(studyView)
        studyView.snp.makeConstraints { (make) in
            make.top.equalTo(topWhiteView.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
//            make.height.equalTo(65)
            make.height.equalTo(0)

        }
        studyView.isHidden = true
        studyView.setNeedsLayout()
        studyView.layoutIfNeeded()
        studyView.image = UIImage(named: "首页蒙版")
        studyView.isUserInteractionEnabled = true
        return studyView
    }()
    
    lazy var studyViewL: UILabel = {
        let studyViewL = UILabel()
        studyView.addSubview(studyViewL)
        studyViewL.font = UIFont.systemFont(ofSize: 16)
        studyViewL.textColor = UIColor.init(lgf_HexString: "3986C0")
        studyViewL.text = "心电摩擦音专场之常见病例分析会"

        studyViewL.numberOfLines = 2
        return studyViewL
    }()
    
    lazy var studyViewStartBtn: UIButton = {
        let studyViewStartBtn = UIButton()
        studyView.addSubview(studyViewStartBtn)
        studyViewStartBtn.setTitle("立即学习", for: .normal)
        studyViewStartBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        studyViewStartBtn.addTarget(self, action: #selector(startStudyClick(btn:)), for: .touchUpInside)
        studyViewStartBtn.setBackgroundImage(UIImage(named: "立即学习"), for: .normal)
        studyViewStartBtn.isHidden = true
        return studyViewStartBtn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        topWhiteView.backgroundColor = .white
        self.addSubview(bottomWhiteView)
        bottomWhiteView.snp.makeConstraints { (make) in
            make.top.equalTo(studyView.snp.top).offset(0)
            make.left.right.bottom.equalToSuperview()
        }

        studyViewL.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.centerY.equalToSuperview()
            make.right.equalTo(studyViewStartBtn.snp.left).offset(-5)
            
        }
        studyViewStartBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-28)
            make.width.equalTo(90)
            make.height.equalTo(30)
        }
        studyView.backgroundColor = .white

        let btnspace = (UIDevice.lgf_ScreenW - 30.0 - 55.0 * 5.0) / 4.0
        let imagesNames = ["公开课程","名医直播","一起学习","成长路径","等级评测"]
        let titles = ["公开课程","名医直播","一起学习","成长路径","等级评测"]

        for i in 0..<5 {
            let image = UIImage(named: imagesNames[i])!
            let titleImage = LGFImageTitleView(image, image, titles[i], titles[i], CGSize(width: 55, height: 55), 3.5)
            let left = 15.0  + btnspace * CGFloat(i) + 55.0 * CGFloat(i)
            self.addSubview(titleImage)

            titleImage.snp.makeConstraints { (make) in
                make.width.equalTo(55)
                make.height.equalTo(92)
                make.top.equalTo(studyView.snp.bottom).offset(0)
                make.left.equalTo(left)
            }
            titleImage.lgf_Name = imagesNames[i]
            titleImage.delegate = self
        }
        
        todayStudyL.text = "今日已学"
        punchcardL.text = "打卡天数"

    }
    
    @objc func startStudyClick(btn:UIButton){
        print("立即学习")
        startStudyClickClouse?()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CourseHeaderView: LGFImageTitleViewDelegate{
    
    func selectLGFImageTitleView(_ view: LGFImageTitleView) {
        btnClickClouse?(view)
    }
}
